package com.bitin.common.validate;

import com.bitin.common.annotation.Phone;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: Mr.Lu
 * @Datetime 2024/8/13 21:01
 */
public class PhoneValidator implements ConstraintValidator<Phone, String> {
    @Override
    public void initialize(Phone constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String phone, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isNotBlank(phone)){
            String regex = "^1(3|4|5|6|7|8|9)\\d{9}$";
            return phone.matches(regex);
        }
        return true;
    }
}
